<header class="row">
  <div class="col-sm-5">
    <h3><%= t('FailedJobs') %></h3>
  </div>
  <% if @failures.size > 0 && @total_size > @count %>
    <div class="col-sm-4">
      <%= erb :_paging, :locals => { :url => "#{root_path}failures" } %>
    </div>
  <% end %>
  <%= filtering('failures') if respond_to?(:filtering) %>
</header>

<% if @failures.size > 0 %>
  <form action="<%= root_path %>failures" method="post">
    <%= csrf_tag if respond_to?(:csrf_tag) %>
    <table class="table table-striped table-bordered table-white">
      <thead>
        <tr>
          <th width="20px" class="table-checkbox">
            <label>
              <input type="checkbox" class="check_all" />
            </label>
          </th>
          <th><%= t('FailedAt') %></th>
          <th><%= t('Queue') %></th>
          <th><%= t('Worker') %></th>
          <th><%= t('Arguments') %></th>
          <th><%= t('Error') %></th>
        </tr>
      </thead>
      <% @failures.each do |entry| %>
        <tr>
          <td class="table-checkbox">
            <label>
              <input type='checkbox' name='key[]' value='<%= job_params(entry.item, entry.score) %>' />
            </label>
          </td>
          <td><a href="<%= root_path %>failures/<%= job_params(entry.item, entry.score) %>"><%= safe_relative_time(entry['failed_at']) %></a></td>
          <td>
            <a href="<%= root_path %>queues/<%= entry.queue %>"><%= entry.queue %></a>
          </td>
          <td><%= entry.respond_to?(:display_class) ? entry.display_class : entry.klass %></td>
          <td>
            <div class="args"><%= display_args(entry.respond_to?(:display_args) ? entry.display_args : entry.args) %></div>
          </td>
          <td style="overflow: auto; padding: 10px;">
            <a class="backtrace" href="#" onclick="$(this).next().toggle(); return false">
              <%= h entry['error_class'] %>: <%= h entry['error_message'].size > 500 ? entry['error_message'][0..500] + '...' : entry['error_message'] %>
            </a>
            <pre style="display: none; background: none; border: 0; width: 100%; max-height: 30em; font-size: 0.8em; white-space: nowrap; overflow: auto;">
              <%= entry['error_backtrace'].join("<br />") if entry['error_backtrace'] %>
            </pre>
            <p>
              <span>Processor: <%= entry['processor'] %></span>
            </p>
          </td>
        </tr>
      <% end %>
    </table>
    <input class="btn btn-primary btn-xs pull-left" type="submit" name="retry" value="<%= t('RetryNow') %>" />
    <input class="btn btn-danger btn-xs pull-left" type="submit" name="delete" value="<%= t('Delete') %>" />
  </form>

  <form action="<%= root_path %>failures/all/delete" method="post">
    <%= csrf_tag if respond_to?(:csrf_tag) %>
    <input class="btn btn-danger btn-xs pull-right" type="submit" name="delete" value="<%= t('DeleteAll') %>" data-confirm="<%= t('AreYouSure') %>" />
  </form>
  <form action="<%= root_path %>failures/all/retry" method="post">
    <%= csrf_tag if respond_to?(:csrf_tag) %>
    <input class="btn btn-danger btn-xs pull-right" type="submit" name="retry" value="<%= t('RetryAll') %>" data-confirm="<%= t('AreYouSure') %>" />
  </form>

  <% if @failures.size > 0 && @total_size > @count %>
    <div class="col-sm-4">
      <%= erb :_paging, :locals => { :url => "#{root_path}failures" } %>
    </div>
  <% end %>

<% else %>
  <div class="alert alert-success"><%= t('NoFailedJobsFound') %></div>
<% end %>
<form action="<%= root_path %>failures/all/reset" method="post">
  <%= csrf_tag if respond_to?(:csrf_tag) %>
  <input class="btn btn-danger btn-xs pull-right" type="submit" name="reset" value="<%= t('Reset Counter') %>" data-confirm="<%= t('AreYouSure') %>" />
</form>
